/**
 * @(#)Connect.java 2011-10-27 Copyright 2011 it.kedacom.com, Inc. All rights
 *                  reserved.
 */

package com.kedacom.ldap.unboundidsdk.demo;

import java.io.IOException;

import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;
import com.unboundid.ldap.sdk.controls.SubentriesRequestControl;
import com.unboundid.ldif.LDIFException;
import com.unboundid.ldif.LDIFWriter;

/**
 * (用一句话描述类的主要功能)
 * @author xiaojinghai
 * @date 2011-10-27
 */

public class ExportDominoUser {

	/**
	 * (用一句话描述方法的主要功能)
	 * @param args
	 */

	public static void main(String[] args) {
		String host = "172.16.30.2";
		int port = 389;
		LDAPConnection conn = null;
		String exportFile = "ExportUsersFromDomino.ldif";
		String mailPart = "@kedacom.com";
		try {
			// conn = new LDAPConnection(host, port, bindDN, password);
			conn = new LDAPConnection(host, port);
			int count = 0;

			// 建立起始节点
			SearchRequest searchRequest;
			searchRequest = new SearchRequest("O=kedacom", SearchScope.SUB, "(objectClass=dominoPerson)", "cn", "sn",
					"o", "ou", "uid", "mail", "objectClass");
			searchRequest.addControl(new SubentriesRequestControl());
			SearchResult searchResult;
			LDIFWriter ldifWriter = new LDIFWriter(exportFile);
			Entry kedacomEntry = new Entry("dn: o=kedacom,dc=com", "objectClass: organization", "objectClass: top",
					"o: kedacom");

			Entry peopleEntry = new Entry("dn: ou=people,o=kedacom,dc=com", "objectClass: organizationalUnit",
					"objectClass: top", "ou: people");
			ldifWriter.writeEntry(kedacomEntry);
			ldifWriter.writeEntry(peopleEntry);

			searchResult = conn.search(searchRequest);
			for (SearchResultEntry e : searchResult.getSearchEntries()) {
				count++;
				Entry en = e.duplicate();

				String userDN = ("cn=" + e.getAttributeValue("cn") + ",ou=people,o=kedacom,dc=com").toLowerCase();

				en.setDN(userDN);
				String[] vs = en.getAttributeValues("objectclass");

				for (String v : vs) {
					v = v.toLowerCase();
					if (v.startsWith("domino")) {
						en.removeAttributeValue("objectclass", v);
					}
				}

				// 剔除非法的email
				String mail = en.getAttributeValue("mail");
				if (mail != null && mail.indexOf("/") != -1) {
					String cn = en.getAttributeValue("cn");
					// 若有中文则转拼音
					String newmail = Pinyin4jUtil.getPinYin(cn) + mailPart;
					// en.removeAttribute("mail");
					en.setAttribute("mail", newmail);
					System.out.println(mail + "-->" + newmail);
				}

				// 替换为正式邮件
				mail = mail.substring(0, mail.indexOf("@")) + mailPart;
				en.setAttribute("mail", mail);

				// 使用Eamil名称取代uid(部份uid是中文)
				String uid = mail.substring(0, mail.indexOf("@"));
				en.setAttribute("uid", uid);

				mail = en.getAttributeValue("mail");

				// 增加shadowAccount对象标识
				en.addAttribute("objectClass", "shadowAccount");
				en.setAttribute("shadowExpire", "99999");
				en.setAttribute("shadowFlag", "0");
				en.setAttribute("shadowInactive", "99999");
				en.setAttribute("shadowLastChange", "12011");
				en.setAttribute("shadowMax", "99999");
				en.setAttribute("shadowMin", "0");
				en.setAttribute("shadowWarning", "0");
				// 初始化密码
				en.setAttribute("userPassword", "{MD5}ChE+9rYYINqlYRyHDtjV7g==");

				ldifWriter.writeEntry(en);

			}
			ldifWriter.close();
			System.out.println("已导出:" + count);
		} catch (IOException e) {
			// TODO 尚未处理异常
			e.printStackTrace();
		} catch (LDAPException e) {
			// TODO 尚未处理异常
			e.printStackTrace();
		} catch (LDIFException e) {
			// TODO 尚未处理异常
			e.printStackTrace();
		}

	}
}
